package com.ruoyi.controller;

import com.ruoyi.response.Response;
import com.ruoyi.response.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

/**
 * 未使用
 * @Author:zzm
 * @Date: 2024/8/28 16:46
 */
@RestController
@RequestMapping("/amac/average")
public class AverageController {

    @GetMapping("/wareHouseAge")
    public Response<?> wareHouseAge(){
        return ResponseUtil.success("666666666");
    }

    /**
     clientId JREO
     clientSecret b0814921f910d35721d187f5c2155255

     */
    /**
     * 获取token
     * @return
     */
    public Response<?> getToken(String client_id,String client_secret){
        try {
            // 创建 URL 对象
            URL url = new URL("/auth/token");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            // 构建 JSON 请求体
            String jsonInputString = String.format("{\"client_id\": \"%s\", \"client_secret\": \"%s\"}", client_id, client_secret);

            // 发送请求体
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 读取响应
            int status = connection.getResponseCode();
            if (status == HttpURLConnection.HTTP_OK) {
                try (Scanner scanner = new Scanner(connection.getInputStream())) {
                    String responseBody = scanner.useDelimiter("\\A").next();
                    System.out.println("Token Response: " + responseBody);
                }
            } else {
                try (Scanner scanner = new Scanner(connection.getErrorStream())) {


                    String errorResponse = scanner.useDelimiter("\\A").next();
                    System.out.println("Error Response: " + errorResponse);
                }
            }
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }


}
